#ifndef CODELIST_H
#define CODELIST_H

#include <QAbstractListModel>
#include <QList>
#include <QString>
#include <QHash>
#include <QByteArray>
#include <QDateTime>
#include <QDebug>
//#include "SqliteHelper.h"

namespace Moolink {


    class Code
    {
    public:

        Code(QVariant codeVar,int Dindex){

            id=codeVar.value<QVariantMap>()["codeId"].toString();
            code=codeVar.value<QVariantMap>()["hjCode"].toString();
            codeOld=codeVar.value<QVariantMap>()["hjCodeOld"].toString();
            codeName=codeVar.value<QVariantMap>()["hjCodeName"].toString();
            unit=codeVar.value<QVariantMap>()["unit"].toString();
            unitTotal=codeVar.value<QVariantMap>()["unitTotal"].toString();
            dataFormat=codeVar.value<QVariantMap>()["dataFormat"].toString();
            deviceId=codeVar.value<QVariantMap>()["deviceId"].toString();
            rtdDataCh=codeVar.value<QVariantMap>()["rtdDataCh"].toInt();
            deviceIndex=Dindex;
            //qDebug()<<code<<":"<<dataFormat;
        }

    public:
        QString id;
        QString code;
        QString codeOld;
        QString codeName;
        QString unit;
        QString unitTotal;
        QString dataFormat;
        QString colorCode="gray";
        int rtdDataCh;
        QString deviceId;
        int deviceIndex;
        QString SystemTime;
        QString dataString="等待采样";
        QChar dataFlag='B';
        QVariantMap cpData;
        qint64 rtdTimeSec=0;
    };

    class CodeList : public QAbstractListModel
    {

        Q_OBJECT

    public:
    //    QString name;
    //    QString portname;
    //    QString protocol;
    //    int comaddr;
    //    QString colorCode;
        enum LineInfo_Roles{
            RoleCodeId =Qt::UserRole + 1,
            RoleCodeName,
            RoledataString
        };


    public:
        explicit CodeList(QObject *parent = 0);

        int rowCount(const QModelIndex &parent) const;
        int rowCountRead();
        QVariant data(const QModelIndex &index, int role) const;
        QHash<int, QByteArray> roleNames() const;

        int getdataSize(){return code_list.size();}
        void addCode(QVariant codeVar,int Dindex=-1);
        void changeDataString(int index,QString str,int error=0);


        Code getdataByIndex(int index){return code_list[index];}
        Code* getpdataByIndex(int index){return &code_list[index];}

    signals:
        void dataChanged();
    public slots:

    private:
        QList<Code> code_list;
        QHash<int, QByteArray> m_roleNames;
    };
}
#endif // CODELISTMODEL_H
